﻿namespace WMS6.BLL
{
    using System;
    using System.Collections.Generic;
    using System.Data;
    using System.Data.Common;
    using WMS6.Common;
    using WMS6.DataEntity;
    using WMS6.DBUtility;
    using WMS6.IDAL;
    using WMS6.QueryEntity;
    using WMS6.DALFactory;

    public class EquipmentTypeBLL : IBaseInfo
    {
        private static readonly ICommonMethodDA commonMethod = DataAccess.CreateCommonMethod();
        private List<string> dataBaseID;
        private static readonly IEquipmentTypeDA EquipmentTypeDA = DataAccess.CreateEquipmentTypeDA();
        private PubParasInfo pubParasInfo;

        public EquipmentTypeBLL()
        {
            this.pubParasInfo = null;
        }

        public EquipmentTypeBLL(string currentWH)
        {
            this.pubParasInfo = null;
            if (!string.IsNullOrEmpty(currentWH))
            {
                this.dataBaseID = commonMethod.GetDataBaseID(currentWH);
                if (this.dataBaseID == null)
                {
                    PublicMethod.GetInstance().ShowAlertMessage("C001");
                }
            }
            else
            {
                PublicMethod.GetInstance().ShowAlertMessage("C002");
            }
        }

        public EquipmentTypeBLL(PubParasInfo tempPubParasInfo)
        {
            this.pubParasInfo = null;
            if (!string.IsNullOrEmpty(tempPubParasInfo.CurrentWH))
            {
                this.dataBaseID = commonMethod.GetDataBaseID(tempPubParasInfo.CurrentWH);
                if (this.dataBaseID == null)
                {
                    PublicMethod.GetInstance().ShowAlertMessage("C001", this.pubParasInfo.UserLanguage);
                }
                else
                {
                    this.pubParasInfo = new PubParasInfo();
                    this.pubParasInfo = tempPubParasInfo;
                }
            }
            else
            {
                PublicMethod.GetInstance().ShowAlertMessage("C002", this.pubParasInfo.UserLanguage);
            }
        }

        public string Delete(List<string> IDS)
        {
            string temp = string.Empty;
            DataBase dataBase = new DataBase();
            DbConnection conn = dataBase.connection;
            conn.Open();
            DbTransaction tran = conn.BeginTransaction();
            try
            {
                List<EquipmentTypeInfo> equipmentTypeInfos = new List<EquipmentTypeInfo>();
                EquipmentTypeInfo equipmentTypeInfo = null;
                foreach (string id in IDS)
                {
                    equipmentTypeInfo = EquipmentTypeDA.GetEquipmentTypeByID(dataBase, tran, id, this.dataBaseID[0]);
                    equipmentTypeInfos.Add(equipmentTypeInfo);
                }
                AuditDataBLL auditDataBLL = new AuditDataBLL();
                if (EquipmentTypeDA.DeleteEquipmentType(dataBase, tran, IDS, this.dataBaseID) > 0)
                {
                    for (int i = 0; i < IDS.Count; i++)
                    {
                        auditDataBLL.InsertAuditData<EquipmentTypeInfo>(dataBase, tran, equipmentTypeInfos[i], null, AuditAction.Delete, this.pubParasInfo.UserID, "EQUIPMENT_TYPE", this.dataBaseID);
                        temp = temp + IDS[i] + ",";
                    }
                    temp = temp.Substring(0, temp.Length - 1);
                }
                tran.Commit();
            }
            catch (Exception ex)
            {
                tran.Rollback();
                CommonErrorHandler.ErrorHandler(ex, LogType.Error);
            }
            finally
            {
                conn.Close();
            }
            return temp;
        }

        public DataSet GetAllInfo()
        {
            return EquipmentTypeDA.GetAllEquipmentType(this.dataBaseID[0]);
        }

        public object GetInfoByID(string id)
        {
            return EquipmentTypeDA.GetEquipmentTypeByID(id, this.dataBaseID[0]);
        }

        public DataSet GetInfoByQueryEntity(object obj)
        {
            EquipmentTypeQueryEntity equipmentTypeQuery = obj as EquipmentTypeQueryEntity;
            List<string> sqlWhere = new List<string>();
            if (!string.IsNullOrEmpty(equipmentTypeQuery.EquipmentTypeID))
            {
                sqlWhere.Add(" EQUIPMENT_TYPE_ID " + equipmentTypeQuery.EquipmentTypeID);
            }
            if (!string.IsNullOrEmpty(equipmentTypeQuery.Descr))
            {
                sqlWhere.Add(" DESCR " + equipmentTypeQuery.Descr);
            }
            if (!string.IsNullOrEmpty(equipmentTypeQuery.CubicCapacity))
            {
                sqlWhere.Add(" CUBIC_CAPACITY " + equipmentTypeQuery.CubicCapacity);
            }
            if (!string.IsNullOrEmpty(equipmentTypeQuery.WeightCapacity))
            {
                sqlWhere.Add(" WEIGHT_CAPACITY " + equipmentTypeQuery.WeightCapacity);
            }
            if (!string.IsNullOrEmpty(equipmentTypeQuery.Length))
            {
                sqlWhere.Add(" LENGTH " + equipmentTypeQuery.Length);
            }
            if (!string.IsNullOrEmpty(equipmentTypeQuery.Width))
            {
                sqlWhere.Add(" WIDTH " + equipmentTypeQuery.Width);
            }
            if (!string.IsNullOrEmpty(equipmentTypeQuery.Height))
            {
                sqlWhere.Add(" HEIGHT " + equipmentTypeQuery.Height);
            }
            if (!string.IsNullOrEmpty(equipmentTypeQuery.IsActive))
            {
                sqlWhere.Add(" IS_ACTIVE " + equipmentTypeQuery.IsActive);
            }
            return EquipmentTypeDA.GetEquipmentTypeByQueryList(sqlWhere, equipmentTypeQuery, this.dataBaseID[0]);
        }

        public string Insert(object obj)
        {
            string EquipmentTypeID = string.Empty;
            DataBase dataBase = new DataBase();
            DbConnection conn = dataBase.connection;
            conn.Open();
            DbTransaction tran = conn.BeginTransaction();
            try
            {
                EquipmentTypeInfo equipementType = obj as EquipmentTypeInfo;
                if (EquipmentTypeDA.CheckEquipmentTypeIDUnique(equipementType.EquipmentTypeID, this.dataBaseID[0]))
                {
                    PublicMethod.GetInstance().ShowAlertMessage("F2530_001", this.pubParasInfo.UserLanguage, new List<string> { equipementType.EquipmentTypeID });
                }
                equipementType.UpdatedBy = equipementType.CreatedBy;
                equipementType.CreatedDate = DateTime.Now;
                equipementType.UpdatedDate = DateTime.Now;
                if (EquipmentTypeDA.InsertEquipmentType(equipementType, this.dataBaseID) > 0)
                {
                    new AuditDataBLL().InsertAuditData<EquipmentTypeInfo>(dataBase, tran, null, equipementType, AuditAction.Insert, this.pubParasInfo.UserID, "EQUIPMENT_TYPE", this.dataBaseID[0]);
                    EquipmentTypeID = equipementType.EquipmentTypeID;
                }
                tran.Commit();
            }
            catch (Exception ex)
            {
                tran.Rollback();
                CommonErrorHandler.ErrorHandler(ex, LogType.Error);
            }
            finally
            {
                conn.Close();
            }
            return EquipmentTypeID;
        }

        public string Update(object obj)
        {
            string equipmentTypeID = string.Empty;
            DataBase dataBase = new DataBase();
            DbConnection conn = dataBase.connection;
            conn.Open();
            DbTransaction tran = conn.BeginTransaction();
            try
            {
                EquipmentTypeInfo equipementType = obj as EquipmentTypeInfo;
                equipementType.UpdatedDate = DateTime.Now;
                EquipmentTypeInfo oldEquipementType = EquipmentTypeDA.GetEquipmentTypeByID(dataBase, tran, equipementType.EquipmentTypeID, this.dataBaseID[0]);
                if (EquipmentTypeDA.UpdateEquipmentType(dataBase, tran, equipementType, this.dataBaseID) > 0)
                {
                    new AuditDataBLL().InsertAuditData<EquipmentTypeInfo>(dataBase, tran, oldEquipementType, equipementType, AuditAction.Update, this.pubParasInfo.UserID, "EQUIPMENT_TYPE", this.dataBaseID);
                    equipmentTypeID = equipementType.EquipmentTypeID;
                }
                tran.Commit();
            }
            catch (Exception ex)
            {
                tran.Rollback();
                CommonErrorHandler.ErrorHandler(ex, LogType.Error);
            }
            finally
            {
                conn.Close();
            }
            return equipmentTypeID;
        }
    }
}

